package com.baiwei.vpnapi.controller;
import com.baiwei.vpnapi.entity.reqentity.UserCreateInput;
import com.baiwei.vpnapi.service.AccountService;
import com.baiwei.vpnapi.utils.AppUtils;
import com.baiwei.vpnapi.utils.ResultVoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.naming.spi.ResolveResult;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @program: vpnapi
 * @description: 账号操作
 * @author: Mr. Stone
 * @create: 2020-02-25 06:58
 **/
@RestController
@RequestMapping("/user")
public class AccountController {

    @Autowired
    private AccountService acService;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @PostMapping("/register")
    public Object register(@RequestBody UserCreateInput info) {
//        String ipAddr = AppUtils.getIpAddr(request);
//        if(ipAddr != null && ipAddr.length() > 0) {
//            String timeStamp = stringRedisTemplate.opsForValue().get("blk:" + ipAddr);
//            if(timeStamp != null && timeStamp.length() > 0) {
//                return ResultVoUtils.fail("注册过于频繁，稍候重试", 300003);
//            } else {
//                String toCatchTime = String.valueOf(new Date().getTime() / 1000);
//                stringRedisTemplate.opsForValue().set("blk:" + ipAddr, toCatchTime, 10, TimeUnit.SECONDS);
//            }
//        }
        return ResultVoUtils.success(acService.userCreate(info));
    }

    @GetMapping("/info")
    public Object userInfo() {
        return ResultVoUtils.success(acService.userInfo());
    }
}
